--
-- Author: yawei.zhu
-- Date: 2018-10-05 11:31:19
--
-- GoddessWishRender
--
local GoddessWishBaseRender = import(".GoddessWishBaseRender")
local NewRareInfoViewEx = require("app.newBag.views.NewRareInfoViewEx")

local ClassRef = app.mvc.createRenderClass(...)

local wordDescs = {
    L("lua_code_text_490"), 
    L("lua_code_text_23"),
    L("lua_code_text_461"),
    L("lua_code_text_487"),
}


--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.GoddessWishRender))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self._ui.Panel:addClickEventListener(function()
        executeFunc(self._onClick)
    end)

    self._ui.Img:setTouchEnabled(true)
    self._ui.Img:addClickEventListener(function()
        self:showTips()
    end)

    self:refresh()
end

-- 卡牌dropType：1、掉落  2、buff  3、增加次数  4、销毁卡牌
function ClassRef:refresh()
    if not self._data then return end

    local colorType = protobuf.enum_id("pb_goddessbless.CardQuality", self._data.quality)
    -- dump(colorType)
    local bgPath = Res.GoddessWish_Bg[colorType]
    self._ui.Image_s:loadTexture(bgPath)
    self._ui.cell:setColor(Enums.goddessColor[colorType])

    self._ui.icon:setVisible(false)
    self._ui.Img:setVisible(false)

    local goddessConfig = GD:getGoddessCardConfig(self._data.id)
    --dump(goddessConfig.dropType)
    if tonumber(goddessConfig.dropType) == 1 then -- 掉落
        self._ui.icon:setVisible(true)
        local data = UD:parseDropToBagItem(goddessConfig.dropItem, true)
        local render = GoddessWishBaseRender.create(self._ui.icon)
        render:setData(data)
        render:setTipEnable(true)

        if data.getName then
            self._ui.text_2:setString(data:getName())
        else
            self._ui.text_2:setString("")
        end
    else
        self._ui.Img:setVisible(true)
        -- 使用配置的
        self._ui.Img:loadTexture(goddessConfig.icon)
        
        self._ui.text_2:setString(L(goddessConfig.name))
        self._descs = L(goddessConfig.desc)
    end

    self._ui.text_1:setString(wordDescs[colorType])
    self._ui.bg_tlt:loadTexture(Res.GoddessWishTlt_Bg[colorType])
end

function ClassRef:showTips()
    local data = {}
    data[1] = {string = self._descs}
    local rarePanel = NewRareInfoViewEx.create()
    rarePanel:setData(data)
    UIHandler:addPopupUi(rarePanel)

    -- 点击空白区域关闭
    display.nodeRemoveForNotHit(rarePanel, function()
        rarePanel:removeFromParent()
    end)

    local boundingBox = self._ui.Img:getBoundingBox()
    local worldPosTemp = self._ui.Img:convertToWorldSpace( cc.p(0, 0) )
    boundingBox.x = worldPosTemp.x
    boundingBox.y = worldPosTemp.y

    display.adjustNodePos(boundingBox, rarePanel)

end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
